from graphviz import Source
import os
from  parse_config import Config

def convert_dot_to_png(dot_file_path, output_png_path):
    try:
        # 检查 dot 文件是否存在
        if not os.path.exists(dot_file_path):
            print(f"错误: 文件 {dot_file_path} 未找到。")
            return

        # 读取 dot 文件内容
        with open(dot_file_path, 'r') as file:
            dot_content = file.read()

        # 创建 Source 对象
        src = Source(dot_content)

        # 渲染为 PNG 文件
        output = src.render(filename=output_png_path, format='png', cleanup=True)

        # 捕获输出
        print(f"成功生成 PNG 文件: {output}")
        return output

    except Exception as e:
        print(f"错误: 发生异常 - {e}")
        return None

def main():
    config_path='./config.json'
    config=Config(config_path)
    dot_file=os.path.splitext(config.config['formal_computation_pic_save_path'])[0] + ".dot"
    output_png=os.path.splitext(config.config['formal_computation_pic_save_path'])[0]
    convert_dot_to_png(dot_file, output_png)
    
if __name__ == "__main__":
    main()
    
    